package cn.tedu.baking.service.impl;

import cn.tedu.baking.mapper.ContentMapper;
import cn.tedu.baking.pojo.dto.ContentDTO;
import cn.tedu.baking.pojo.dto.ContentIndexQueryDTO;
import cn.tedu.baking.pojo.dto.ContentManagementQueryDTO;
import cn.tedu.baking.pojo.entity.Content;
import cn.tedu.baking.pojo.vo.ContentDetailVO;
import cn.tedu.baking.pojo.vo.ContentIndexVO;
import cn.tedu.baking.pojo.vo.ContentManagementVO;
import cn.tedu.baking.pojo.vo.ContentUpdateVO;
import cn.tedu.baking.service.ContentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private ContentMapper contentMapper;
    //重新完成之前的addNew()
    @Override
    public void addNew(ContentDTO contentDTO) {
        Content content = new Content();
        BeanUtils.copyProperties(contentDTO,content);
        if(contentDTO.getId()==null){//如果没有传入id,说明是新增
            content.setCreateTime(new Date());
            contentMapper.insert(content);
        }else{//有id说明是修改
            //设置修改时间 调用修改方法 修改方法没有需要新建
            content.setUpdateTime(new Date());
            contentMapper.update(content);
        }
    }

    @Override
    public List<ContentManagementVO> listForManagement(ContentManagementQueryDTO contentManagementQueryDTO) {
        return contentMapper.selectForManagement(contentManagementQueryDTO);
    }

    @Override
    public void deleteById(Long id) {
        contentMapper.deleteById(id);
    }

    @Override
    public ContentUpdateVO getDetailForUpdate(Long id) {
        log.debug("getDetailForUpdate id={}",id);
        return contentMapper.selectForUpdateById(id);
    }

    @Override
    public List<ContentIndexVO> listForIndex(ContentIndexQueryDTO contentIndexQueryDTO) {
        return contentMapper.selectForIndex(contentIndexQueryDTO);
    }

    @Override
    public ContentDetailVO getDetailById(Long id) {
        return contentMapper.selectForDetailById(id);
    }
}
